package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuQuery;
import com.spzx.product.api.domain.vo.SkuPriceVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService productSkuService;

    @Operation(summary = "分页及带条件的查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(Product product) {
        //开启分页
        startPage();
        //调用自定义的带条件查询的方法
        List<Product> productList = productService.getPageList(product);
        return getDataTable(productList);
    }

    @Operation(summary = "添加商品")
    @PostMapping
    public AjaxResult saveProduct(@RequestBody Product product) {
        //调用自定义的保存商品信息的方法
        int flag = productService.saveProduct(product);
        return toAjax(flag);
    }

    @Operation(summary = "根据id查询商品详情")
    @GetMapping("/{id}")
    public AjaxResult getProductById(@PathVariable Long id) {
        //调用自定义的根据id查询商品详情的方法
        Product product = productService.getProductById(id);
        return AjaxResult.success(product);
    }

    @Operation(summary = "根据id更新商品信息")
    @PutMapping
    public AjaxResult updateProductById(@RequestBody Product product) {
        //调用自定义的根据id更新商品信息的方法
        productService.updateProductById(product);
        return AjaxResult.success();
    }

    @Operation(summary = "根据id删除和批量删除商品信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductById(@PathVariable List<Long> ids) {
        //调用自定义的根据id删除的方法
        productService.deleteProductByIds(ids);
        return AjaxResult.success();
    }

    @Operation(summary = "根据商品id和审核状态值更新审核状态")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatusById(@PathVariable Long id, @PathVariable Integer auditStatus) {
        //调用自定义的根据商品id更新审核状态的方法
        productService.updateAuditStatusById(id, auditStatus);
        return AjaxResult.success();
    }

    @Operation(summary = "根据商品id上下架商品")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatusById(@PathVariable Long id, @PathVariable Integer status) {
        //调用自定义的根据商品id上下架商品
        productService.updateStatusById(id, status);
        return AjaxResult.success();
    }


    @InnerAuth
    @Operation(summary = "获取sku销量top20")
    @GetMapping("/getTopSale")
    public R<List<ProductSku>> getTopSale() {
        return R.ok(productService.getTopSale());
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用获取商品列表的接口")
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable("pageNum") Integer pageNum,

            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @PathVariable("pageSize") Integer pageSize,

            SkuQuery skuQuery) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> list = productService.selectProductSkuList(skuQuery);
        return R.ok(getDataTable(list));
    }

    @Operation(summary = "获取商品sku信息")
    @InnerAuth
    @GetMapping(value = "/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId) {
        return R.ok(productSkuService.getById(skuId));
    }

    @Operation(summary = "获取商品信息")
    @InnerAuth
    @GetMapping(value = "/getProduct/{id}")
    public R<Product> getProduct(@PathVariable("id") Long id) {
        return R.ok(productService.getById(id));
    }

    @Operation(summary = "获取商品sku最新价格信息")
    @InnerAuth
    @GetMapping(value = "/getSkuPrice/{skuId}")
    public R<SkuPriceVo> getSkuPrice(@PathVariable("skuId") Long skuId) {
        return R.ok(productService.getSkuPrice(skuId));
    }

    @Operation(summary = "获取商品详细信息")
    @InnerAuth
    @GetMapping(value = "/getProductDetails/{id}")
    public R<ProductDetails> getProductDetails(@PathVariable("id") Long id) {
        return R.ok(productService.getProductDetails(id));
    }

    @Operation(summary = "获取商品sku规则详细信息")
    @InnerAuth
    @GetMapping(value = "/getSkuSpecValue/{id}")
    public R<Map<String, Long>> getSkuSpecValue(@PathVariable("id") Long id) {
        return R.ok(productService.getSkuSpecValue(id));
    }

    @Operation(summary = "获取商品sku库存信息")
    @InnerAuth
    @GetMapping(value = "/getSkuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId) {
        return R.ok(productService.getSkuStock(skuId));
    }

    @Operation(summary = "批量获取商品sku最新价格信息")
    @InnerAuth
    @PostMapping(value = "/getSkuPriceList")
    public R<List<SkuPriceVo>> getSkuPriceList(@RequestBody List<Long> skuIdList) {
        return R.ok(productService.getSkuPriceList(skuIdList));
    }
}
